Đặt lại Git
Đặt lại Git
reset
là lệnh chúng ta sử dụng khi muốn di chuyển kho lưu trữ trở lại commit
trước đó, loại bỏ mọi thay đổi được thực hiện sau commit
đó.
Bước 1: Tìm commit
trước đó:
Bước 2: Di chuyển kho trở lại bước đó:
Sau chương trước, chúng ta có một phần trong lịch sử commit
mà chúng ta có thể quay lại. Hãy thử làm điều đó với reset
.
Đặt lại Git Tìm cam kết trong nhật ký
Việc đầu tiên chúng ta cần tìm ra điểm mà mình muốn quay lại. Để làm điều đó, chúng ta cần phải xem qua nhật log
.
Để tránh danh sách log
quá dài, chúng ta sẽ sử dụng tùy chọn --oneline
, tùy chọn này chỉ cung cấp một dòng cho mỗi commit
hiển thị:
- Bảy ký tự đầu tiên của
commit hash
- đây là những gì chúng ta cần tham khảo trong lệnh đặt lại của mình. - tin
commit message
Vậy hãy tìm điểm mà chúng ta muốn reset
về:
Ví dụ
git log --oneline e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..." 52418f7 Just a regular update, definitely no accidents here... 9a9add8 (origin/master) Added .gitignore 81912ba Corrected spelling error 3fdaa5b Merge pull request #1 from w3schools-test/update-readme 836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world e7de78f Updated index.html. Resized image 5a04b6f Updated README.md with a line about focus d29d69f Updated README.md with a line about GitHub e0b6038 merged with hello-world-images after fixing conflicts 1f1584e added new image dfa79db updated index.html with emergency fix 0312c55 Added image to Hello World 09f4acd Updated index.html with a new line 221ec6e First release of Hello World!
Chúng tôi muốn quay lại commit
: 9a9add8 (origin/master) Added .gitignore
, cái cuối cùng trước khi chúng tôi bắt đầu làm rối tung mọi thứ.
Đặt lại Git
Chúng tôi reset
kho lưu trữ của mình về cam kết cụ thể bằng cách sử dụng git reset commithash
( commithash
là 7 ký tự đầu tiên của hàm băm cam kết mà chúng tôi tìm thấy trong log
):
Ví dụ
git reset 9a9add8
Bây giờ hãy kiểm tra lại log
:
Ví dụ
git log --oneline 9a9add8 (HEAD -> master, origin/master) Added .gitignore 81912ba Corrected spelling error 3fdaa5b Merge pull request #1 from w3schools-test/update-readme 836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world e7de78f Updated index.html. Resized image 5a04b6f Updated README.md with a line about focus d29d69f Updated README.md with a line about GitHub e0b6038 merged with hello-world-images after fixing conflicts 1f1584e added new image dfa79db updated index.html with emergency fix 0312c55 Added image to Hello World 09f4acd Updated index.html with a new line 221ec6e First release of Hello World!
Cảnh báo: Việc xáo trộn lịch sử commit
của kho lưu trữ có thể nguy hiểm. Thông thường, bạn có thể thực hiện những loại thay đổi này đối với kho lưu trữ cục bộ của riêng mình. Tuy nhiên, bạn nên tránh thực hiện các thay đổi ghi lại lịch sử vào kho lưu trữ remote
, đặc biệt nếu những người khác đang làm việc với chúng.
Đặt lại hoàn tác Git
Mặc dù các cam kết không còn hiển thị trong log
nhưng nó vẫn không bị xóa khỏi Git.
Nếu bạn biết hàm băm cam kết, bạn có thể reset
lại nó:
Ví dụ
git reset e56ba1f
Bây giờ hãy kiểm tra lại log
:
Ví dụ
git log --oneline e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..." 52418f7 Just a regular update, definitely no accidents here... 9a9add8 (origin/master) Added .gitignore 81912ba Corrected spelling error 3fdaa5b Merge pull request #1 from w3schools-test/update-readme 836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world e7de78f Updated index.html. Resized image 5a04b6f Updated README.md with a line about focus d29d69f Updated README.md with a line about GitHub e0b6038 merged with hello-world-images after fixing conflicts 1f1584e added new image dfa79db updated index.html with emergency fix 0312c55 Added image to Hello World 09f4acd Updated index.html with a new line 221ec6e First release of Hello World!